<?php defined('BASEPATH') or exit('No direct script access allowed');

/**
 * Format Plugin
 *
 * Various text formatting functions.
 *
 * @author  PyroCMS Dev Team
 * @package PyroCMS\Core\Plugins
 */
class Plugin_format extends Plugin
{

	public $version = '1.0.0';
	public $name = array(
		'en' => 'Format',
	);
	public $description = array(
		'en' => 'Format strings, including Markdown and Textile.',
		'br' => 'Formata strings, incluindo Markdown e Textile.',
		'el' => 'Μορφοποίηση κειμενοσειρών, συμπεριλαμβανομένων των Markdown και Textile.',
            'fa' => 'به فرمت در آوردن  رشته های مانندMarkdown و Textile ',
		'fr' => 'Formatter des chaînes de caractères, incluant Markdown et Textile.',
		'it' => 'Formatta le stringhe, incluse Markdown e Textitle',
	);

	/**
	 * Returns a PluginDoc array that PyroCMS uses 
	 * to build the reference in the admin panel
	 *
	 * All options are listed here but refer 
	 * to the Format plugin for a larger example
	 *
	 * @return array
	 */
	public function _self_doc()
	{
		$info = array(
			'markdown' => array(
				'description' => array(
					'en' => 'Send some content through the Markdown processor.',
					'br' => 'Passa algum conteúdo pelo processador Markdown.'
				),
				'single' => false,
				'double' => true,
				'variables' => '',
				'attributes' => array(),
			),
			
			'textile' => array(
				'description' => array(
					'en' => 'Send some content through the Textile processor.',
					'br' => 'Passa algum conteúdo pelo processador Textile.'
				),
				'single' => false,
				'double' => true,
				'variables' => '',
				'attributes' => array(),
			),
			
			'url_title' => array(
				'description' => array(
					'en' => 'A Plugin shortcut to the CodeIgniter url_title() function.',
					'br' => 'Um Plugin de atalho para a função url_title() do CodeIgniter.'
				),
				'single' => true,
				'double' => false,
				'variables' => '',
				'attributes' => array(
					'string' => array(
						'type' => 'text',
						'required' => true,
					),
					'separator' => array(
						'type' => 'text',
						'default' => 'dash',
						'required' => false,
					),
					'lowercase' => array(
						'type' => 'bool',
						'flags' => 'true|false',
						'default' => 'false',
						'required' => false,
					),
				),
			),
		);
	
		return $info;
	}

	/**
	 * Markdown
	 *
	 * Takes content and formats it with the Markdown Library.
	 *
	 * Usage:
	 * {{ format:markdown }}
	 *   Formatted **text**
	 * {{ /format:markdown }}
	 *
	 * Outputs: <p>Formatted <strong>text</strong></p>
	 *
	 * @return string The HTML generated by the Markdown Library.
	 */
	public function markdown()
	{
		$this->load->helper('markdown');

		$content = $this->attribute('content', $this->content());

		return parse_markdown(trim($content));
	}

	/**
	 * Textile
	 *
	 * Takes content and formats it with the Textile Library.
	 *
	 * Usage:
	 * {{ format:textile }}
	 *   Formatted _text_
	 * {{ /format:textile }}
	 *
	 * Outputs: <p>Formatted <em>text</em></p>
	 *
	 * @return string The HTML generated by the Textile Library.
	 */
	public function textile()
	{
		$this->load->library('textile');

		$content = $this->attribute('content', $this->content());

		return $this->textile->TextileThis(trim($content));
	}

	/**
	 * URL Title
	 *
	 * Converts a string using the `url_title()` URL Helper function
	 *
	 * Usage:
	 * {{ format:url_title string="Introducing New Administrators" separator="dash" lowercase="true" }}
	 *
	 * Outputs: "introducing-new-administrators"
	 *
	 * @return string Formatted with the `url_title` helper function
	 */
	public function url_title()
	{
		$this->load->helper('url');

		$attrs = $this->attributes();

		// fix 'true' or 'false' to real bools.
		if (count($attrs) > 2)
		{
			$bool = array_slice($attrs, 2);
			array_splice($attrs, 2, 1, array_map('str_to_bool', $bool));
		}

		return call_user_func_array('url_title', $attrs);
	}

}

/* EOF */
